<template>
  <div class="container my-info">
    <!-- <van-nav-bar
      title="兑换优惠券"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    /> -->
    
    <div class="input-group" style="margin-top:20px;">
      <van-cell-group>
        <van-field
          v-model="Amount"
          label="兑换金额"
          placeholder="请输入兑换金额"
        />
      </van-cell-group>
    </div>
    <div class="exchange-tips">
      温馨提示：当前{{value}}积分可兑换价值1RMB商城优惠券，工作日每天可兑换{{num}}张商城优惠券，总商城优惠券上限金额{{money}}元；周末每天可兑换{{weekNum}}张商城优惠券，总商城优惠券上限金额{{weekMoney}}元。
    </div>
    <div class="my-info-btn-row">
        <van-button type="danger" class="save-btn" @click="exchangeCoupon">兑换</van-button>
    </div>
  </div>
</template>

<script>
//import { msToDate } from '@/until/until';
export default {
  name: 'exchangeCuopon',
  data() {
    return {
      Amount:'',
      userId:'',
      value:'',
      num:'',
      money:'',
      weekNum:'',
      weekMoney:''
    };
  },
  mounted() {
    let userInfo = JSON.parse(localStorage.getItem("userInfo"));
    this.userId = userInfo.Id;
    this.getConfigType();
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    getConfigType(){
      this.$api.mine.getConfigType(0).then(res=> {
        if(res.IsSuccess){
          this.value = res.Data.Value;
        }
      }) 
      this.$api.mine.getConfigType(1).then(res=> {
        if(res.IsSuccess){
          this.num = res.Data.Value;
        }
      }) 
      this.$api.mine.getConfigType(2).then(res=> {
        if(res.IsSuccess){
          this.money = res.Data.Value;
        }
      })  
      this.$api.mine.getConfigType(6).then(res=> {
        if(res.IsSuccess){
          this.weekNum = res.Data.Value;
        }
      }) 
      this.$api.mine.getConfigType(7).then(res=> {
        if(res.IsSuccess){
          this.weekMoney = res.Data.Value;
        }
      })  
    },
    exchangeCoupon(){
      if(this.Amount == ''){
        this.$toast('请输入兑换金额')
        return false;
      }else{
        let myreg=/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;  
        if (!myreg.test(this.Amount)) {  
            this.$toast('兑换金额格式不正确，最多小数点后2位');
            return false;
        }
      }
      let data ={
        Amount:this.Amount
      };
      this.$api.mine.exchangeCoupon(data).then(res=> {
        if(res.IsSuccess){
          this.$toast.success('兑换成功');
          setTimeout(()=>{
            this.$router.go(-1);
          },1000)
        }
      })    
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.my-info {
  .select-time{
    position: absolute;
    bottom: 0;
    width: 100%;
  }
  .my-info-btn-row {
    margin-top: .533333rem;
    text-align: center;
    padding: 0 0.4rem;
    .save-btn {
      width: 100%;
    }
  }
  .exchange-tips {
    font-size: .373333rem;
    padding: .266667rem .4rem;
    color:red;
  }
}
</style>